﻿using bridge.Silverlight.Helpers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HuobiDetail
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Top = 180;
            Rectangle ScreenArea = System.Windows.Forms.Screen.GetBounds(this);
            int width1 = ScreenArea.Width;
            Left = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - 300;
            this.FormBorderStyle = FormBorderStyle.None;

            Timer timer = new Timer();
            timer.Interval = 500;
            timer.Tick += Timer_Tick;
            timer.Start();


        }
       
        private async void Timer_Tick(object sender, EventArgs e)
        {
         
            try
            {

                double bagsNum = double.Parse(Readjson("bagsNum"));

                var valBags = await HttpHelper.Get<AjaxDto>("https://api.huobi.be/market/detail/merged?symbol=bagsusdt");
                labVal.Text = valBags.tick.close;

                var valbtc = await HttpHelper.Get<AjaxDto>("https://api.huobi.be/market/detail/merged?symbol=btcusdt");
                labelBtc.Text = valbtc.tick.close;

                //获取usdt - rmb的价格
                var valUsdtToRmb = await HttpHelper.Get<ToRmbAjaxDto>("https://otc-api.huobi.be/v1/data/market/detail");
                List<DetailDto> detailList = valUsdtToRmb.data.detail;
                //取出 coinName = USDT 的
                DetailDto usdtRmb = detailList.FirstOrDefault(r => r.coinName.Equals("USDT"));
                double usdtToRmb = double.Parse(null == usdtRmb ? "0.0" : usdtRmb.sell);


                //我持bags币的价值
                Lab_amount.Text = (double.Parse(valBags.tick.close) * bagsNum * usdtToRmb).ToString("f2");

            }
            catch (Exception ex)
            {

            }

        }

        private void close_Click(object sender, EventArgs e)
        {
            //关闭程序
            Application.Exit();
        }

        [DllImport("user32.dll")]//拖动无窗体的控件
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        { 
            //拖动窗体
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);

        }

        /// <summary>
        /// 读取JSON文件
        /// </summary>
        /// <param name="key">JSON文件中的key值</param>
        /// <returns>JSON文件中的value值</returns>
        public static string Readjson(string key)
        {
            //获取本地json文件
            string jsonfile = Directory.GetCurrentDirectory() + "//json.json";


            using (System.IO.StreamReader file = System.IO.File.OpenText(jsonfile))
            {
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JObject o = (JObject)JToken.ReadFrom(reader);
                    var value = o[key].ToString();
                    return value;
                }
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        { 
            //获取本地json文件
            string jsonfile = Directory.GetCurrentDirectory() + "//json.json";
            System.Diagnostics.Process.Start("notepad.exe", jsonfile);

        }
    }
}
